package com.wll.ec.core.net.interceptors;

import android.annotation.SuppressLint;

import com.wll.ec.core.util.storage.EcPreference;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * created by detachment on 2020/3/22
 */
@SuppressWarnings({"NullableProblems", "ResultOfMethodCallIgnored"})
public final class AddCookieInterceptor implements Interceptor {

    @SuppressLint("CheckResult")
    @Override
    public Response intercept(Chain chain) throws IOException {
        final Request.Builder builder = chain.request().newBuilder();
        Observable.just(EcPreference.getCustomAppProfile("cookie"))
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String cookie) throws Exception {
                        // 给原生API请求附带上webview拦截的cookie
                        builder.addHeader("Cookie",cookie);
                    }
                });
        return chain.proceed(builder.build());
    }
}
